﻿@{
    ViewData["Title"] = "语言标签管理";
    Layout = "~/Views/Shared/_ManageLayout.cshtml";
}
@section css{
    <style>
        .layui-table-cell {
            line-height: 35px;
        }
    </style>
}
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">语言：</label>
                        <div class="layui-input-inline mr0">
                            <select id="languageId" name="languageId">
                                <option value="">请选择</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">标签Key：</label>
                        <div class="layui-input-inline mr0">
                            <input id="key" name="key" class="layui-input" type="text" />
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">标签值：</label>
                        <div class="layui-input-inline mr0">
                            <input id="value" name="value" class="layui-input" type="text" />
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button class="layui-btn icon-btn" lay-filter="formSubSearchTbBas" lay-submit>
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>
                        <a class="layui-btn icon-btn" id="addlanguageText">
                            <i class="layui-icon">&#xe654;</i>新增标签
                        </a>
                    </div>
                </div>
            </div>

            <table class="layui-table" lay-even lay-skin="line" lay-size="lg" id="tableTbBas" lay-filter="tableTbBas"></table>
        </div>
    </div>
</div>
<!-- 表格操作列 -->
<script type="text/html" id="tableBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<!-- 表单弹窗 -->
<script type="text/html" id="modelAuth">
    <form id="languageTextForm" lay-filter="languageTextForm" class="layui-form model-form">
        <input type="hidden" name="id" id="id" value="0">
        <div class="layui-form-item">
            <label class="layui-form-label">语言:</label>
            <div class="layui-input-block">
                <select id="language" name="languageId">
                    <option value="">请选择</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">标签Key:</label>
            <div class="layui-input-block">
                <input name="key" placeholder="请输入标签Key" type="text" class="layui-input" 
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">标签值:</label>
            <div class="layui-input-block">
                <input name="value" placeholder="请输入标签值" type="text" class="layui-input"
                       lay-verType="tips" lay-verify="required" required />
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closePageDialog">取消</button>
            <button class="layui-btn" lay-filter="savelanguageText" lay-submit>保存</button>
        </div>
    </form>
</script>
<!-- js部分 -->
<script>
    layui.use(['admin', 'layer', 'form', 'table', 'laydate', 'laytpl','upload'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var laydate = layui.laydate;
        var admin = layui.admin;
        var laytpl = layui.laytpl;
        var upload = layui.upload;
        @Html.LayuiRenderSelect("/api/LanguageType/QueryLanguageSelect", "languageId")
        // 渲染表格
        var insTb = table.render({
            elem: '#tableTbBas',
            url: '/api/languageText',
            page: true,
            limit: 30,
            cellMinWidth: 100,
            cols: [[
                { field: 'languageName', align: 'center', title: '语言' },
                { field: 'key', align: 'center', title: '标签Key' },
                { field: 'value', align: 'center', edit: 'text', title: '标签值' },
                { templet: '#tableBar', title: '操作', align: 'center', minWidth: 160 }
            ]]
        });

        //监听工具条
        table.on('tool(tableTbBas)', function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值
            if (layEvent == "edit") {
                admin.open({
                    area: ["500px", "500px"],
                    type: 1,
                    title: '修改标签[' + data.key + '(' + data.languageName+'站)]',
                    content: $('#modelAuth').html(),
                    success: function (layero, dIndex) {
                        @Html.LayuiRenderSelect("/api/LanguageType/QueryLanguageSelect", "language",IsAsync:false)
                        form.val('languageTextForm', data);
                        // 表单提交事件
                        form.on('submit(savelanguageText)', function (rowData) {
                            layer.load(2);
                            $.ajax({
                                url: "/api/languageText/"+data.id,
                                type: 'put',
                                contentType: "application/json;charset=utf-8",
                                data: JSON.stringify(rowData.field),
                                dataType: "json",
                                complete: function (obj) {
                                    layer.closeAll('loading');
                                },
                                success: function (res) {
                                    if (res.success) {
                                        layer.msg(res.message, { icon: 1 });
                                        insTb.reload({}, 'data');
                                        layer.close(dIndex);
                                    } else {
                                        layer.msg(res.message, { icon: 2 });
                                    }
                                }
                            });
                            return false;
                        });
                    }
                });
            } else if (layEvent == "del") {
                layer.confirm('确定要删除“[' + data.key + '(' + data .languageName +'站)]”吗,删除后，可能会出现网站信息不全？', {
                    skin: 'layui-layer-admin',
                    shade: .1
                }, function (i) {
                    layer.load(2);
                    $.ajax({
                        url: '/api/languageText/' + data.id,
                        type: 'delete',
                        async: true,
                        dataType: 'json',
                        complete: function (obj) {
                            layer.closeAll('loading');
                        },
                        success: function (result) {
                            layer.closeAll('loading');
                            if (result.success) {
                                layer.msg(result.message, { icon: 1 });
                                insTb.reload({}, 'data');
                            } else {
                                layer.msg(result.message, { icon: 2 });
                            }
                        }
                    });
                });
            }
        });
        //监听单元格编辑
        table.on('edit(tableTbBas)', function (obj) {
            var data = obj.data;
            layer.load(2);
            $.ajax({
                url: '/api/languageText/'+data.id,
                type: 'put',
                contentType: "application/json;charset=utf-8",
                data: JSON.stringify(data),
                dataType: "json",
                async: true,
                complete: function (obj) {
                    layer.closeAll('loading');
                },
                error: function (xhr) {
                    layer.msg("输入有误，请重新输入", { icon: 2 });
                    insTb.reload();
                },
                success: function (res) {
                    if (res.success) {
                        layer.msg(res.message, { icon: 1 });
                        insTb.reload();
                    } else {
                        layer.msg(res.message, { icon: 2 });
                    }
                }
            });
        });
        // 搜索按钮点击事件
        form.on('submit(formSubSearchTbBas)', function (data) {
            insTb.reload({ where: data.field, page: { curr: 1 } }, 'data');
        });
        //新增任务
        $("#addlanguageText").click(function () {
            admin.open({
                area: ["500px", "500px"],
                type: 1,
                title: '新标签',
                content: $('#modelAuth').html(),
                success: function (layero, dIndex) {
                    @Html.LayuiRenderSelect("/api/LanguageType/QueryLanguageSelect", "language")
                    // 表单提交事件
                    form.on('submit(savelanguageText)', function (rowData) {
                        layer.load(2);
                        $.ajax({
                            url: '/api/languageText',
                            type: 'post',
                            contentType: "application/json;charset=utf-8",
                            data: JSON.stringify(rowData.field),
                            dataType: "json",
                            async: true,
                            complete: function (obj) {
                                layer.closeAll('loading');
                            },
                            success: function (res) {
                                if (res.success) {
                                    layer.msg(res.message, { icon: 1 });
                                    insTb.reload({}, 'data');
                                    layer.close(dIndex);
                                } else {
                                    layer.msg(res.message, { icon: 2 });
                                }
                            }
                        });
                        return false;
                    });
                }
            });
        });
    });
</script>

